//
//  ProfileViewController.m
//  moon
//
//  Created by beyself on 16/6/15.
//  Copyright © 2016年 beyself. All rights reserved.
//

#import "Utils.h"
#import "ProfileViewController.h"

@interface ProfileViewController ()

@end

@implementation ProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    
    UIButton* button  = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 10, SCREEN_WIDTH, 48);
    button.titleLabel.font = FONT_15;
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitleColor:TEXT forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"清除缓存" forState:UIControlStateNormal];
    
    UIButton* arrow = [UIButton buttonWithType:UIButtonTypeCustom];
    arrow.frame = CGRectMake(SCREEN_WIDTH - 48, 0, 48, 48);
    [arrow setImage:[UIImage imageNamed:@"ic_arrow"] forState:UIControlStateNormal];
    [button addSubview:arrow];
    
    CALayer* layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 47.5, SCREEN_WIDTH, .5);
    layer.backgroundColor = LINE.CGColor;
    [button.layer addSublayer:layer];
    
    layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 0, SCREEN_WIDTH, .5);
    layer.backgroundColor = LINE.CGColor;
    [button.layer addSublayer:layer];
    [self.view addSubview:button];
    
    button  = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 58, SCREEN_WIDTH, 48);
    button.titleLabel.font = FONT_15;
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitleColor:TEXT forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(cleanFeedback) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"清空所有回执" forState:UIControlStateNormal];
    
    arrow = [UIButton buttonWithType:UIButtonTypeCustom];
    arrow.frame = CGRectMake(SCREEN_WIDTH - 48, 0, 48, 48);
    [arrow setImage:[UIImage imageNamed:@"ic_arrow"] forState:UIControlStateNormal];
    [button addSubview:arrow];
    
    layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 47.5, SCREEN_WIDTH, .5);
    layer.backgroundColor = LINE.CGColor;
    [button.layer addSublayer:layer];
    
    [self.view addSubview:button];
    
    button  = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 116, SCREEN_WIDTH, 48);
    button.titleLabel.font = FONT_15;
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitleColor:TEXT forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(setSilent) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"推送静音" forState:UIControlStateNormal];
    
    int silence = [APPDELEGATE.mMemberInfo[@"is_silent"] intValue];
    
    arrow = [UIButton buttonWithType:UIButtonTypeCustom];
    arrow.frame = CGRectMake(SCREEN_WIDTH - 48, 0, 48, 48);
    arrow.tag = 2000;
    [arrow setImage:[UIImage imageNamed:@"check_win"] forState:UIControlStateNormal];
    [arrow setImage:[UIImage imageNamed:@"check_win_focus"] forState:UIControlStateSelected];
    [arrow addTarget:self action:@selector(setSilent) forControlEvents:UIControlEventTouchUpInside];
    if(silence) arrow.selected = YES;
    [button addSubview:arrow];
    
    layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 0, SCREEN_WIDTH, .5);
    layer.backgroundColor = LINE.CGColor;
    [button.layer addSublayer:layer];
    
    layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 47.5, SCREEN_WIDTH, .5);
    layer.backgroundColor = LINE.CGColor;
    [button.layer addSublayer:layer];
    
    [self.view addSubview:button];
}

- (void) setSilent
{
    UIButton* btn = (UIButton*)[self.view viewWithTag:2000];
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setObject:APPDELEGATE.mMemberInfo[@"id"] forKey:@"id"];
    if(btn.selected) [param setObject:@"0" forKey:@"is_silent"];
    else [param setObject:@"1" forKey:@"is_silent"];
    [Utils sendHttpPost:param method:@"set_silent" controller:self flag:1];
}

- (void) cleanFeedback
{
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setObject:APPDELEGATE.mMemberInfo[@"id"] forKey:@"id"];
    [param setObject:[APPDELEGATE.mMainViewController.mSelectClass componentsJoinedByString:@","] forKey:@"class_id"];
    [Utils sendHttpPost:param method:@"send_feedback_all" controller:self flag:0];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    if(flag == 0)
    {
        [self showAllTextDialog:@"清除成功"];
        
        MainViewController* subview = APPDELEGATE.mMainViewController;
        [APPDELEGATE setTabInit:YES tab:0];
        [APPDELEGATE setTabInit:YES tab:1];
        [APPDELEGATE setTabInit:YES tab:2];
        [APPDELEGATE setTabInit:YES tab:3];
        [subview buttonClick:(UIButton*)[subview.view viewWithTag:2000]];
    }
    else if(flag == 1)
    {
        ((UIButton*)[self.view viewWithTag:2000]).selected = !((UIButton*)[self.view viewWithTag:2000]).selected;
        
        NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
        [param setObject:[Utils readDefaultUser:@"username"] forKey:@"phonenumber"];
        [param setObject:[Utils readDefaultUser:@"password"] forKey:@"password"];
        [Utils sendHttpPost:param method:@"member_login" controller:self flag:2];
    }
    else
    {
        [Utils saveCustomObject:data[@"member"] key:LOGIN_INFO];
        [APPDELEGATE.mMemberInfo setDictionary:data[@"member"]];
    }
}

- (void) buttonClick:(UIButton*)btn
{
    [self showAllTextDialog:@"清除成功"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
